﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Collections;

namespace InfoPathTranslate
{
    class InfoPathXml
    {
        public static String GetNodeAttr(XmlNode node, String name)
        {
            for (int i = 0; i < node.Attributes.Count; i++)
            {
                XmlNode n = node.Attributes.Item(i);
                if (n.Name.Equals(name))
                {
                    return n.Value;
                }
            }
            return null;
        }

        public static void SetNodeAttr(XmlNode node, String name, String value)
        {
            XmlAttribute attr = node.Attributes[name];
            if (attr == null)
            {
                attr = node.OwnerDocument.CreateAttribute(name);
                node.Attributes.Append(attr);
            }
            attr.Value = value.Trim() ;
        }

        public static void MoveNodeAttrs(XmlNode fromNode,XmlNode toNode)
        {
            for (int i = 0; i < fromNode.Attributes.Count; i++)
            {
                XmlAttribute attr = fromNode.Attributes[i];
                toNode.Attributes.Append(attr);
            }
        }

        public static XmlNode GetSubNode(XmlNode node, String name)
        {
            for (int i = 0; i < node.ChildNodes.Count; i++)
            {
                XmlNode n = node.ChildNodes[i];
                if (n.Name.Equals(name))
                {
                    return n;
                }
            }
            return null;
        }
    }
}
